package client;
import ucm.UCConnectClient;
import server.Server;

public class Client {
    private static Client instance = null;
    private static String user = null;
    private String permission = null;    

    public Client() {
        // connect to server
        ClientView.getInstance();
        new UCConnectClient().run();
    }

    public static Client getInstance() {
        if (instance == null) {
            instance = new Client();
        }
        return instance;
    }    

    public void setUser(String username) {
        user = username;
        Server.addUser(username);
    }

    public void setUserPermission (String userType) {
        permission = userType;
    }

    public String getPermission() {
        return permission;
    }

    public static String getUser() {
        if (user != "admin") {
            return user;
        }
        else {
            return "";
        }
    }

    public void logIn(String userType) {
        if (user != null) {
            ClientView.printMessage("User already logged in. Please logout and try again.\n");
        }
        else {
            Login.loadLoginUserView(userType);
        }
    }

    public void logout() {
        if (user != null) {
            ClientView.printMessage("User: " + user + " logged out\n");
            //remove users status from server
            Server.removeUser(user);
            //remove users status from client
            user = null;
            ClientView.getInstance().setView("Default");            
        }
        else {
            ClientView.printMessage("No user logged in\n");
        }
    }
}
